﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using FaultBackService.Shared;

namespace FaultBackService.DAL
{
    class t_TurbDataGeneric
    {
        /// <summary>
        /// 获取最新周期所有风机的十分钟数据
        /// </summary>
        /// <param name="currentDBTime">当前数据库时间</param>
        /// <returns></returns>
        public IList<Models.t_TurbDataGenericViewModel> GetLastList(out DateTime currentDBTime)
        {

            using (Models.SiteDataContext context = new Models.SiteDataContext())
            {
                currentDBTime = GetCurrentTimeOfDB(context);

                //最新记录的时间戳
                DateTime lastTStampe = GetLastTimeStamp(context);

                DateTime startTime = lastTStampe - new TimeSpan(0, 1, 0);

                //与最新时间戳相差少于1分钟的记录列表
                return context.t_TurbDataGeneric
                    .OrderByDescending(d => d.TStampOfMidPoint)
                    //时间和最新时间戳相差小于1分钟
                    .Where(d => d.TStampOfMidPoint > startTime)
                    .Select(d => new Models.t_TurbDataGenericViewModel
                    {
                        NodeID = d.NodeID,
                        TStampOfMidPoint = d.TStampOfMidPoint,
                        GBoxDegC_Mean = d.GBoxDegC_Mean,
                        GenDegC_Mean = d.GenDegC_Mean
                    })
                    .ToList();


            }
        }

        /// <summary>
        /// 获取指定时间区间内的数据集合,按时间降序排列
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        internal IList<Models.t_TurbDataGenericViewModel> GetViewList(DateTime startTime, DateTime endTime)
        {
            using (Models.SiteDataContext context = new Models.SiteDataContext())
            {
                return context.t_TurbDataGeneric
                    .OrderByDescending(d => d.TStampOfMidPoint)
                    .Where(d => d.TStampOfMidPoint > startTime && d.TStampOfMidPoint < endTime)
                    .Select(d=>new Models.t_TurbDataGenericViewModel {
                        TStampOfMidPoint=d.TStampOfMidPoint,
                        NodeID=d.NodeID,
                        GBoxDegC_Mean=d.GBoxDegC_Mean,
                        GenDegC_Mean=d.GenDegC_Mean
                    }).ToList();
            }
        }

        /// <summary>
        /// 获取指定时间区间内的数据集合,按时间升序排列
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        internal IList<Models.t_TurbDataGeneric2> GetViewList2(DateTime startTime, DateTime endTime)
        {
            using (Models.SiteDataContext context = new Models.SiteDataContext())
            {
                return context.t_TurbDataGeneric
                    .OrderBy(d => d.TStampOfMidPoint)
                    .Where(d => d.TStampOfMidPoint > startTime && d.TStampOfMidPoint < endTime)
                    .Select(d => new Models.t_TurbDataGeneric2
                    {
                        SiteID=d.SiteID,
                        NodeID = d.NodeID,
                        TStampOfMidPoint = d.TStampOfMidPoint,
                        kW_Mean=d.kW_Mean,
                        kVArMean=d.kVArMean,
                        NacWSpeedMean_mps=d.NacWSpeedMean_mps,
                        NacOrientationMean_degs=d.NacOrientationMean_degs,
                        RotorRPM_Mean=d.RotorRPM_Mean,
                        PitchMean_degs=d.PitchMean_degs,
                        GBoxDegC_Mean = d.GBoxDegC_Mean,
                        GenDegC_Mean = d.GenDegC_Mean
                    }).ToList();
            }
        }


        /// <summary>
        /// 获取最新的数据时间,如表中无数据，返回默认时间
        /// </summary>
        /// <returns></returns>
        internal DateTime GetLastTimeStamp()
        {
            using (Models.SiteDataContext context = new Models.SiteDataContext())
            {
                var data= context.t_TurbDataGeneric
                    .OrderByDescending(d => d.TStampOfMidPoint)
                    .FirstOrDefault();
                if (data==null)
                {
                    return Global.DefaultTime;
                }
                else
                {
                    return data.TStampOfMidPoint;
                }
            }
        }

        /// <summary>
        /// 获取表t_TurbDataGeneric的最新一条记录的TStampOfMidPoint值
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private DateTime GetLastTimeStamp(Models.SiteDataContext context)
        {
            return context.t_TurbDataGeneric
                                .OrderByDescending(d => d.TStampOfMidPoint)
                                .First()
                                .TStampOfMidPoint;
        }

        /// <summary>
        /// 获取服务器当前时间
        /// </summary>
        /// <param name="context"></param>
        private DateTime GetCurrentTimeOfDB(DbContext context)
        {
            return context.Database.SqlQuery<DateTime>("Select GetDate();").Single();
        }
    }
}
